package com.aaron.kjviewutils.car;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aaron.kjviewutils.R;
import com.aaron.kjviewutils.RecycleViewDividerForList;
import com.aaron.kjviewutils.eventBus.BundleEventMsg;
import com.aaron.kjviewutils.stock.StockInnerAdapter;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by Aaron on 2017/3/2.
 */

public class CarFragment extends Fragment {

    @Bind(R.id.carsRecyclerView)
    RecyclerView carsRecyclerView;

    private CarsActivity rootAty;
    private View rootView;
    private CarsAdapter adapter;
    private List<CarBean.CarInfo.CarInner> datas;
    private CarBean.CarInfo carInfo;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        rootAty = (CarsActivity) getActivity();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.frgment_cars, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initContent();
    }

    private void initContent()
    {
        Bundle bundle = getArguments();
        if (bundle != null) {
            carInfo = (CarBean.CarInfo) bundle.getSerializable("carInfo");
            if(null!=carInfo)
            {
                datas = carInfo.son;
                // 错列网格布局
                carsRecyclerView.setLayoutManager(new LinearLayoutManager(rootAty));
                adapter = new CarsAdapter(rootAty, rootAty, datas);
                carsRecyclerView.setAdapter(adapter);
                carsRecyclerView.setItemAnimator(new DefaultItemAnimator()); // 默认动画
                carsRecyclerView.addItemDecoration(new RecycleViewDividerForList(rootAty, LinearLayoutManager.HORIZONTAL));
                adapter.setmOnItemClickListener(new StockInnerAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        CarBean.CarInfo.CarInner carInner = datas.get(position);
                        if(null!=carInner)
                        {
                            Bundle bundle = new Bundle();
                            bundle.putString("name", carInner.type);
                            rootAty.eventBus.post(new BundleEventMsg(BundleEventMsg.EventBusType.BUNDLE_CAR_INFO, bundle));
                        }
                    }
                });
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        rootAty = null;
    }
}
